package com.gc.web.controller.business;

import cn.hutool.core.date.DateUtil;
import com.gc.business.domain.Attachment;
import com.gc.business.domain.IntegralGoods;
import com.gc.business.service.IAttachmentService;
import com.gc.business.service.IIntegralGoodsService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 积分商品 控制层
 * 
 * @author liujinteng
 * @date 2021-01-04
 */
@Slf4j
@Controller
@RequestMapping("/business/integralGoods")
public class IntegralGoodsController extends BaseController {
    private String prefix = "business/integralGoods";
	
	private final IIntegralGoodsService integralGoodsService;

	private final IAttachmentService attachmentService;

	@Autowired
	public IntegralGoodsController(IIntegralGoodsService integralGoodsService,
								   IAttachmentService attachmentService) {
		this.integralGoodsService = integralGoodsService;
		this.attachmentService = attachmentService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:integralGoods:view")
	@GetMapping
	public String integralGoods(){
	    return prefix + "/integralGoods";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{goodsId}")
	public String edit(@PathVariable("goodsId") Long goodsId, ModelMap mmap){
		IntegralGoods integralGoods = integralGoodsService.getById(goodsId);
		List<Attachment> attachmentList = new ArrayList<>();
		if (StringUtils.isNotEmpty(integralGoods.getGoodsRolling())){
			if(integralGoods.getGoodsRolling().contains(",")){
				String[] split = integralGoods.getGoodsRolling().split(",");
				for (String attachmentId : split) {
					Attachment attachment = attachmentService.getById(Long.valueOf(attachmentId));
					if(StringUtils.isNotNull(attachment)){
						attachmentList.add(attachment);
					}
				}
			}else {
				Attachment attachment = attachmentService.getById(Long.valueOf(integralGoods.getGoodsRolling()));
				if(StringUtils.isNotNull(attachment)){
					attachmentList.add(attachment);
				}
			}
			mmap.put("attachmentList",attachmentList);
		}
		mmap.put("integralGoods", integralGoods);
		return prefix + "/edit";
	}
	
	/**
	 * 查询积分商品列表
	 */
	@RequiresPermissions("business:integralGoods:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(IntegralGoods integralGoods){
		integralGoods.setCompanyId(ShiroUtils.getSysUser().getAttrId());
		startPage();
        List<IntegralGoods> list = integralGoodsService.selectGoodsByCompanyId(integralGoods.getCompanyId(),integralGoods.getGoodsName(),integralGoods.getGoodsStatus());
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存积分商品
	 */
	@RequiresPermissions("business:integralGoods:add")
	@Log(title = "积分商品", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(IntegralGoods integralGoods){
		integralGoods.setCompanyId(ShiroUtils.getSysUser().getAttrId());
		integralGoods.setCreateTime(DateUtil.date());
		integralGoods.setCreateBy(ShiroUtils.getSysUser().getUserName());
		integralGoodsService.save(integralGoods);
		return Result.success();
	}

	/**
	 * 修改保存积分商品
	 */
	@RequiresPermissions("business:integralGoods:edit")
	@Log(title = "积分商品", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(IntegralGoods integralGoods){
		integralGoods.setUpdateBy(ShiroUtils.getSysUser().getUserName());
		integralGoods.setUpdateTime(DateUtil.date());
		integralGoodsService.updateById(integralGoods);
		return Result.success();
	}
	
	/**
	 * 删除积分商品
	 */
	@RequiresPermissions("business:integralGoods:remove")
	@Log(title = "积分商品", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		integralGoodsService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出积分商品列表
	 */
	@RequiresPermissions("business:integralGoods:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(IntegralGoods integralGoods){
		List<IntegralGoods> list = integralGoodsService.list(integralGoods);
		ExcelUtil<IntegralGoods> util = new ExcelUtil<>(IntegralGoods.class);
		util.exportExcel(list, "integralGoods");
		return Result.success();
	}

	/**
	 * 下架积分商品
	 */
	@RequiresPermissions("business:integralGoods:undercarriage")
	@PostMapping("/undercarriage")
	@ResponseBody
	public Result undercarriage(Long goodsId){
		IntegralGoods integralGoods = integralGoodsService.getById(goodsId);
		if (integralGoods == null){
			return Result.failed("该商品不存在");
		}
		//下架
		integralGoods.setGoodsStatus("1");
		boolean undercarriage = integralGoodsService.updateById(integralGoods);
		if (undercarriage){
			return Result.success();
		}
		return Result.failed();
	}

	/**
	 * 上架积分商品
	 */
	@RequiresPermissions("business:integralGoods:shelves")
	@PostMapping("/shelves")
	@ResponseBody
	public Result shelves(Long goodsId){
		IntegralGoods integralGoods = integralGoodsService.getById(goodsId);
		if (integralGoods == null){
			return Result.failed("该商品不存在");
		}
		//上架
		integralGoods.setGoodsStatus("0");
		boolean shelves = integralGoodsService.updateById(integralGoods);
		if (shelves){
			return Result.success();
		}
		return Result.failed();
	}
}
